// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import java.util.Enumeration;
import java.util.Properties;

import org.dengues.commons.connectivity.IConnectivitySettingsConstants;
import org.dengues.core.components.ENodeParameterName;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.prefs.connectivity.EConnectivityProperties;
import org.dengues.core.prefs.connectivity.models.ConnectivityProperty;
import org.dengues.core.property.IPropertyController;
import org.dengues.core.property.NodeParameterUtils;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.cmd.PropertyChangeTextCommand;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public abstract class AbstractPropertyController implements IPropertyController {

    public static final int STANDARD_HEIGHT = 16;

    public static final int BUTTON_HEIGHT = 18;

    public static final int COMBO_WIDTH = 40;

    public static final int STANDARD_LABEL = 60;

    public static final int STANDARD_WIDTH = 100;

    protected static final String PARAMETER_NAME = "PARAMETER_NAME"; //$NON-NLS-1$

    protected PropertyTabDynamicSection dps;

    protected String paramName;

    public void setDynamicPropertySection(PropertyTabDynamicSection dps) {
        this.dps = dps;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AbstractPropertyController constructor comment.
     */
    public AbstractPropertyController(PropertyTabDynamicSection dps) {
        this.dps = dps;
    }

    public void initialize() {

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getWidgetFactory".
     * 
     * @return
     */
    protected TabbedPropertySheetWidgetFactory getWidgetFactory() {
        return dps.getWidgetFactory();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.property.IPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    public Control createControl(final Composite composite, final NodePropertyParameter parameter, final Control lastControl) {
        Composite subcomposite = new Composite(composite, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;
        subcomposite.setLayout(gridLayout);
        subcomposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        subcomposite.setBackground(composite.getBackground());
        return subcomposite;
    }

    protected String[] getDisplayItems(NodePropertyParameter param) {
        return NodeParameterUtils.getDisplayItems(param);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "register".
     * 
     * @param param
     * @param labelText
     */
    protected void register(final NodePropertyParameter param, final Control labelText) {
        labelText.addKeyListener(new KeyAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
             */
            @Override
            public void keyReleased(KeyEvent e) {
                Command command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), ((Text) labelText).getText());
                getCommandStack().execute(command);
            }

        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCommandStack".
     * 
     * @return
     */
    protected CommandStack getCommandStack() {
        MultiPageEditorPart part = dps.getCurrentEditor();
        if (part == null) {

        }
        Object adapter = part.getAdapter(CommandStack.class);
        return (CommandStack) adapter;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "updateComponentVisiable".
     * 
     * @param paramName
     * @param displayName
     */
    public void updateComponentVisiable() {
        if (ENodeParameterName.CONNECTIVITY.getName().equals(paramName) && dps.getElem() instanceof CompNode) {
            CompNode compNode = (CompNode) dps.getElem();
            NodePropertyParameter elementParameter = compNode.getElementParameter(paramName);
            if (elementParameter != null && elementParameter.getValue() instanceof String) {
                ConnectivityProperty connectivityProperty = NodeParameterUtils.getConnectivityProperty((String) elementParameter
                        .getValue());
                if (connectivityProperty == null) {
                    return;
                }
                Properties baseProperties2 = connectivityProperty.getBaseProperties();
                Enumeration baseProperties = baseProperties2.keys();
                for (EConnectivityProperties property : EConnectivityProperties.values()) {
                    elementParameter = compNode.getElementParameter(property.getParamName());
                    if (elementParameter != null) {
                        elementParameter.setShow(false);
                        elementParameter.setValue("");
                    }
                }
                while (baseProperties.hasMoreElements()) {
                    String id = (String) baseProperties.nextElement();
                    if (id.startsWith(IConnectivitySettingsConstants.PROP_PREFIX)) {
                        String string = id.substring(IConnectivitySettingsConstants.PROP_PREFIX.length());
                        elementParameter = compNode.getElementParameter(string.toUpperCase());
                        if (elementParameter != null) {
                            elementParameter.setShow(true);
                            elementParameter.setValue(baseProperties2.get(id));
                        }

                    }
                }
            }
        }
    }
}
